<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ include file="/WEB-INF/view/include/inc.jsp"%>
<!DOCTYPE html >
<html>
<head>
<%@ include file="/WEB-INF/view/jQgrid.jsp"%>
</head>
<body style="background-color: white;" class="gray-bg">
	<div class="ibox-content">
		<div class="row-fluid">
			<div class="col-sm-12">
				<input type="hidden" id="tempids" value="${ids }" />
		        <input type="hidden" id="tempnames" value="" /> 
				<form class="form-horizontal m-t " id="signupForm" method="post"
					enctype="multipart/form-data">
					<input type="hidden" id="talents_id" name="talentsId" value="${talentsId}" />
					<fieldset>
						<legend>培训经历</legend>
						<div class="workinput">
						<c:forEach items="${trainedus }" var="trainedu" varStatus="num">
						<div class="addexp${num.count }">
						    <input type="hidden" id="id${num.count }" name="id${num.count }" value="${trainedu.id }">
							<div class="form-group">
								<label class="col-sm-3 control-label">起止时间：</label>
								<div class="col-sm-8 form-inline">
									<input type="text" class="form-control datetime" id="t_startDate${num.count }" name="startDate${num.count }" value="${trainedu.startDate }"  style="width: 40%" readonly="readonly" placeholder="请选择起始时间"/> -
									<input type="text" class="form-control datetime" id="t_endDate${num.count }" name="endDate${num.count }" value="${trainedu.endDate }"  style="width: 40%" readonly="readonly" placeholder="请选择截止时间"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">培训机构：</label>
								<div class="col-sm-8">
									<input type="text"  class="form-control company" id="t_schoolName${num.count }" name="schoolName${num.count }" value="${trainedu.schoolName }"/>
									<input type="hidden"  class="form-control" id="t_schoolId${num.count }" name="schoolId${num.count}" value="${trainedu.schoolId }"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">培训课程：</label>
								<div class="col-sm-8">
									<input type="text" class="form-control" id="e_course${num.count }" name="course${num.count }" value="${trainedu.course }"  />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">获得证书：</label>
								<div class="col-sm-8">
									<input type="text" class="form-control" id="t_grade${num.count }" name="grade${num.count }" value="${trainedu.grade }"  />
								</div>
							</div>
							<div class="form-group" style="text-align: center;">
								<div class="col-sm-12">
								    <button type="button" class="btn btn-primary btn-sm closebtn" onclick="removehtml(${num.count });"><i class="icon-minus"></i>删除</button>
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-12" style="border: 1px solid #e5e6e7;">
								</div>
							</div>
						</div>
						</c:forEach>
						<c:if test="${trainnum ==0 }">
						<input type="hidden" name="id" value="">
						<div class="form-group">
								<label class="col-sm-3 control-label">起止时间：</label>
								<div class="col-sm-8 form-inline">
									<input type="text" class="form-control datetime" id="t_startDate1" name="startDate1"  style="width: 40%" readonly="readonly" placeholder="请选择起始时间"/> -
									<input type="text" class="form-control datetime" id="t_endDate1" name="endDate1"  style="width: 40%" readonly="readonly" placeholder="请选择截止时间"/>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">培训机构：</label>
								<div class="col-sm-8">
									<input type="text"  class="form-control company" id="t_schoolName1" name="schoolName1" />
									<input type="hidden"  class="form-control" id="t_schoolId1" name="schoolId1" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">培训课程：</label>
								<div class="col-sm-8">
									<input type="text" class="form-control" id="e_course1" name="course1" />
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">获得证书：</label>
								<div class="col-sm-8">
									<input type="text" class="form-control" id="t_grade1" name="grade1" />
								</div>
							</div>
							<div class="form-group">
								<div class="col-sm-12" style="border: 1px solid #e5e6e7;">
								</div>
							</div>
						</c:if>
						</div>
					</fieldset>
                    <div class="form-group" style="text-align: center;">
							<div class="col-sm-12">
								<button type="button" class="btn btn-primary btn-sm addbtn"
									id="addinput">
									<i class="icon-plus"></i>增加培训经历
								</button>
							</div>
					</div>
					<div class="form-group" style="text-align: center;">
						<div class="col-sm-12">
							<button class="btn btn-primary" type="button" id="sub">提交</button>
							<button class="btn btn-primary" type="button"
								onclick="top.Dialog.close();">取消</button>
						</div>
					</div>
				</form>
			</div>
		</div>
	</div>
	<script type="text/javascript">
	 $(function () {
		 $("#t_schoolName1").autosuggest({
	            url: '<%=contextPath%>/school/getJsonSchool?type=2',
	                minLength: 1,
	                maxNum: 4,
	                align: 'center',
	                method: 'post',
	                highlight: true,
	                immediate: true,
	                queryParamName: 'name',
	                nextStep: function (ele) {
	                    var tid = ele.data('id'); 
	                    $("#t_schoolId1").val(tid);
	                },
	                split: ' ',
	                open: function(){
	                    //console.log("start open");
	                },
	                close: function(){
	                    //console.log("start close");
	                }
	            });
		 $('.datetime').datetimepicker({
			    format: 'yyyy-mm',
		        autoclose: true,
		        todayBtn: true,
		        startView: 'year',
		        minView:'year',
		        maxView:'decade',
		        language:  'zh-CN',
	    	    autoclose: 1//选择后自动关闭
		  });
		 //$("#t_startDate1").datetimepicker("update",'2002-01-01');//赋值
		 $('#t_startDate1').datetimepicker({
	    	 format: 'yyyy-mm',
		        autoclose: true,
		        todayBtn: true,
		        startView: 'year',
		        minView:'year',
		        maxView:'decade',
		        language:  'zh-CN',
	    	    autoclose: 1//选择后自动关闭
	  }).on('hide', function(event) {
		  var year = event.date.getFullYear();//获取年
		  var newyear = year+3;
		  $("#t_endDate1").datetimepicker("update",newyear+'-01');//赋值
		});
		 var y=1;
		 var trainnum = ${trainnum};
		 if(trainnum == 0){y=2};
		 if(trainnum > 0){y= trainnum+y};
		 $("#addinput").click(function(){
			 var html='<div class="addexp'+y+'"><input type="hidden" name="id'+y+'" value=""><div class="form-group"><label class="col-sm-3 control-label">起止时间：</label><div class="col-sm-8 form-inline">';
			       html+='<input type="text" class="form-control datetime" id="t_startDate'+y+'" name="startDate'+y+'"  style="width: 40%" readonly="readonly" placeholder="请选择起始时间"/> -';
			       html+='<input type="text" class="form-control datetime" id="t_endDate'+y+'" name="endDate'+y+'"  style="width: 40%" readonly="readonly" placeholder="请选择截止时间"/></div></div>';
				   html+='<div class="form-group"><label class="col-sm-3 control-label">培训机构：</label><div class="col-sm-6">';
				   html+='<input type="text"  class="form-control company" id="t_schoolName'+y+'" name="schoolName'+y+'" />';
				   html+='	<input type="hidden"  class="form-control" id="t_schoolId'+y+'" name="schoolId'+y+'" /></div></div>';
				   html+='<div class="form-group"><label class="col-sm-3 control-label">培训课程：</label>';
				   html+='<div class="col-sm-8"><input type="text" class="form-control" id="t_course'+y+'" name="course'+y+'" /></div></div>';
				   html+='<div class="form-group"><label class="col-sm-3 control-label">获得证书：</label><div class="col-sm-8">';
				   html+='<input type="text" class="form-control" id="t_grade'+y+'" name="grade'+y+'" /></div></div>';
		           html +='<div class="form-group" style="text-align: center;"><div class="col-sm-12"><button type="button" class="btn btn-primary btn-sm closebtn" onclick="removehtml('+y+');"><i class="icon-minus"></i>删除</button></div></div>'
		           html+='<div class="form-group"><div class="col-sm-12" style="border: 1px solid #e5e6e7;"></div></div></div>';
				 y =y+1;
			 $(html).appendTo(".workinput");
			 var num = y-1;
			 var old = y-2;
			 $("#t_schoolName"+num).autosuggest({
		            url: '<%=contextPath%>/school/getJsonSchool?type=2',
		                minLength: 1,
		                maxNum: 4,
		                align: 'center',
		                method: 'post',
		                highlight: true,
		                immediate: true,
		                queryParamName: 'name',
		                nextStep: function (ele) {
		                    var tid = ele.data('id'); 
		                    $("#t_schoolId"+num).val(tid);
		                },
		                split: ' ',
		                open: function(){
		                    //console.log("start open");
		                },
		                close: function(){
		                    //console.log("start close");
		                }
		            });
			 $('.datetime').datetimepicker({
		    	 format: 'yyyy-mm',
			        autoclose: true,
			        todayBtn: true,
			        startView: 'year',
			        minView:'year',
			        maxView:'decade',
			        language:  'zh-CN',
		    	    autoclose: 1//选择后自动关闭
		  });
			 var time = $("#t_startDate"+old).val();
			 var oldyear = time.substring(0,4);//获取上个日期年
			 var newyear = time.substring(0,4)-4;//获取年
			 $("#t_endDate"+num).datetimepicker("update",oldyear+'-01');//赋值
			 $("#t_startDate"+num).datetimepicker("update",newyear+'-01');//赋值
		    $('#t_startDate'+num).datetimepicker({
		    	 format: 'yyyy-mm',
			        autoclose: true,
			        todayBtn: true,
			        startView: 'year',
			        minView:'year',
			        maxView:'decade',
			        language:  'zh-CN',
		    	    autoclose: 1//选择后自动关闭
		  }).on('hide', function(event) {
			  //var year = event.date.getFullYear();//获取年
			  //var newyear = year+4;
			  //$("#t_endDate"+num).datetimepicker("update",newyear+'-01-01');//赋值
			});
		 });
	        $("#sub").click(function () {
	            $("#signupForm").ajaxSubmit({
	                url: '<%=contextPath%>/edu/updatetrain',
	                data:{num:y},
					type : 'post',
					dataType : "json",
					beforeSend: function () {
				        // 禁用按钮防止重复提交
						$("#sub").attr("disabled","disabled");
						swal("信息已经提交，请等待后台处理~！");
				    },
					success : function(data) {
						if (data.code == 200) {
							swal({
								title : data.message,
								type : "success",
								showCancelButton : false,
								confirmButtonText : "确定",
								animation : "slide-from-bottom",
								text: "2秒后自动关闭。", 
		      	    			timer: 2000, 
								closeOnConfirm : true
							}, function() {
								top.Dialog.close();
							});
						} else {
							swal("错误", data.message, "error");
						}  
					},    
					complete: function () {
				        $("#sub").removeAttr("disabled");
				    },
				    error: function (data) {
				    	swal("错误", data.message, "error");
				    }
				});
			});
		});
	 function removehtml(num) {
		 console.log("num值为:"+num);
		 var tid = $("#id"+num).val();
		 console.log("id值为:"+tid);
		 if(tid!=null && tid!=''){
			 swal({
				    title: "您确定要删除该条记录吗",
				    text: "删除后将无法恢复，请谨慎操作！",
				    type: "warning",
				    showCancelButton: true,
				    confirmButtonColor: "#DD6B55",
				    confirmButtonText: "删除",
				    animation:"slide-from-bottom",
				    cancelButtonText:"取消",
				    closeOnConfirm: true
				},function (){
					 $.ajax({
						 url:'<%=contextPath%>/edu/delete?id='+tid,
						 dataType:'json',
						 success:function(data){
							if(data.code==200){
								swal("删除成功");
								$(".addexp"+num).remove();//事先存好了对象就方便删除了
					    	}else{
					    		swal("出现错误，请稍后再试！");
					    	}
						 }
					 });
				});
		 }else{
			 $(".addexp"+num).remove();//事先存好了对象就方便删除了
		 }
	}
	 function addSchool(num) {
		 var name = $("#t_schoolName"+num).val();
		 if(name !=""){
			 $.ajax({
					url:'<%=contextPath%>/school/add',
					data:{classify:2,name:name},
					type:'post',
					dataType:'json',
		      	    success:function(data){
		      	    	if(data.code ==200){
		      	    		swal({
		      	    			title:data.message,
		      	    			text: "1秒后自动关闭。", 
		      	    			timer: 1000, 
		      				    showConfirmButton: false
		      	    		});
		      	    		$("#e_schoolId"+num).val(data.id);
		      	    	}else {
		      	    		swal({
		      	    			title:data.message,
		      	    			text: "1秒后自动关闭。", 
		      	    			timer: 1000, 
		      				    showConfirmButton: false
		      	    		});
		      	    	}
		      	    }
				});
		 }else {
			 swal({
	    			title:"名称未填写",
	    		});
		}
	}
	</script>
</body>
</html>